<template>
  <div>
    <div v-for="(file,index) in uploadList" class="upload-list">
      <img :src="file" alt="" />
      <div class="upload-list-cover">
        <span @click="hanldClick(index)" class="modify">点击修改图片</span>
        <i class="remove-img" @click="handleRemove(index)"></i>
      </div>
    </div>

    <input type="file" ref="input" @change="handleChange" style="display: none"/>
    <div @click="hanldClick()" style="display:inline-block" v-show="uploadList.length < 3">
      <slot></slot>
    </div>
  </div>
</template>
<script>
  import ajax from "./ajax.js";
  import vue from "vue";

  export default {
    name: "upload",
    props: {
      action: {
        type: String
      },
      defaultList: {
        type: Array,
        default: []
      },
      isClear: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        uploadList: [],
        uploadingIndex: undefined
      }
    },
    mounted() {
      this.uploadList = this.defaultList;
    },
    watch: {
      isClear() {
        console.log("this.isClear:"+this.isClear)
        this.uploadList = [];
      }
    },
    methods: {
      hanldClick(index) {
        this.$refs.input.click();
        this.uploadingIndex = index;
      },
      handleChange(e) {
        const files = e.target.files;
        console.log(files)
        if (!files) {
          return;
        }
        this.uploadFiles(files);
        this.$refs.input.value = null;

        console.log(files);
      },
      handleRemove(index) {
        this.uploadList.splice(index, 1)
      },
      uploadFiles(files) {
        let postFiles = Array.prototype.slice.call(files);
        if (postFiles.length === 0) return;

        postFiles.forEach(file => {
          this.upload(file);
        });
      },
      upload(file) {
        file.uid = Date.now() + this.tempIndex++;
        // const _file = {
        //   status: 'uploading',
        //   name: file.name,
        //   size: file.size,
        //   percentage: 0,
        //   uid: file.uid,
        //   showProgress: true,
        //   url: ""
        // };
        if (undefined === this.uploadingIndex) {
          this.uploadList.push("");
        } else {
          // todo:这边改为正在上传的状态
          this.uploadList[this.uploadingIndex] = "";
        }
        // console.log(_file)
        let that = this;
        ajax({
          file: file,
          filename: file.name,
          action: that.action,
          onProgress: e => {},
          onSuccess: function (res) {
            let index;
            if (undefined === that.uploadingIndex) {
              index = that.uploadList.length - 1;
            } else {
              index = that.uploadingIndex;
            }

            // that.uploadList.splice(index, 1,
            //   "https://o5wwk8baw.qnssl.com/a42bdcc1178e62b4694c830f028db5c0/avatar");
            that.uploadList.splice(index, 1, res.url);
            // that.$emit("getUploadImg", res.url)
          },
          onError: function (res) {
            let index;
            if (undefined === that.uploadingIndex) {
              index = that.uploadList.length - 1;
            } else {
              index = that.uploadingIndex;
            }
            that.uploadList.splice(index, 1)
          }
        });
      }
    }
  }

</script>

<style lang="less">
  @import "~COMMON/common.less";

  .upload-list {
    position: relative;
    vertical-align: bottom;
    box-sizing: initial;
    display: inline-block;
    width: 72px;
    height: 72px;
    text-align: center;
    border: solid 1px #dcdcdc;
    overflow: hidden;
    // margin-top: 15px;
    margin-right: 15px;
    img {
      width: 100%;
      height: 100%;
    }
    &:nth-of-type(3n) {
      margin-right: 0px;
    }
    &:hover .upload-list-cover {
      display: block;
    }
    .upload-list-cover {
      position: absolute;
      top: 0;
      left: 0;
      display: none;
      color: #fff;
      width: 260px;
      height: 180px;
      background-color: rgba(0, 0, 0, 0.2);
      .modify {
        cursor: pointer;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
      .remove-img {
        position: absolute;
        top: 10px;
        right: 10px;
        cursor: pointer;
        display: inline-block;
        width: 16px;
        height: 20px;
        // background-image: url('~IMGS/stock-pool-remove-white.png')
      }
    }
  }

</style>
